<?php
/**
 * 周舟 hzboye010@163.com
 * addby sublime snippets
 */
namespace app\index\domain;

use app\shop\api\FavoritesApi;
use app\shop\api\ProductApi;
use app\shop\api\ProductFaqApi;
use app\shop\api\ProductGroupApi;
use app\shop\api\ProductSkuApi;
use app\shop\api\ProductAttrApi;
use app\shop\api\ProductPropApi;
use app\shop\api\ProductImageApi;
use app\shop\api\MmzWinnerPepoleApi;
use app\shop\api\MmzApplyPepoleApi;
use app\shop\api\MmzImageApi;
use app\shop\api\AddressApi;
use app\shop\api\ProductServiceApi;
use app\shop\api\ProductMemberPriceApi;
use app\shop\api\SearchWordsApi;
// use Common\Api\AccountApi;
// use Shop\Model\ProductFaqModel;
use app\shop\model\Product;
use app\shop\api\CategoryApi;
use app\shop\api\OrdersItemApi;
use app\system\api\MemberApi;
use app\system\api\UcenterMemberApi;
use app\system\api\UserApi;
use app\system\api\UserGroupApi;
use app\system\model\UserGroup;
use app\system\model\UserLog;
use app\shop\api\StoreApi;
use app\shop\api\StorePromotionApi;
use app\admin\api\FreightTemplateApi;
use app\admin\api\DatatreeApi;
use app\shop\api\ProductPackageItemApi;
use app\shop\api\ProductPackageApi;
use app\shop\api\OrderCommentApi;
use app\shop\api\OrderCommentAttachsApi;
use app\admin\api\MessageApi;
use app\admin\model\Message;

class ProductDomain extends BaseDomain{

    protected $business_code = "product";
    private   $cate;

    public function packageDetail(){
        $this->checkVersion($this->api_ver);
        addLog("Product/packageDetail",$_GET,$_POST,"应用" . $this->client_id . "调用商品套餐查询接口");
        $id = $this->_post('package_id',0,'套餐ID缺失');
        $r = apiCall(ProductPackageApi::PACKAGE_DETAIL,[$id]);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        foreach ($r['info'] as &$v) {

        }
        $this->apiReturnSuc($r['info']);
    }
    //查询商品服务
    public function getServices(){
        $this->checkVersion($this->api_ver);
        addLog("Product/getServices",$_GET,$_POST,"应用" . $this->client_id . "调用商品搜索词查询接口");
        $cacheKey = 'productservice';
        $this->checkCache($cacheKey,1,1);

        $r = apiCall(DatatreeApi::QUERY_NO_PAGING,[['parentid'=>6101],'sort asc','id,name']);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        foreach ($r['info'] as &$v) {
            $v['img'] = getCdnIcon('service',35,$v['id']);
        }
        $this->cache($cacheKey,$r['info'],600);
    }
    //查询商品搜索关键词
    public function searchWords(){
        $this->checkVersion($this->api_ver);
        addLog("Product/searchWords",$_GET,$_POST,"应用" . $this->client_id . "调用商品搜索词查询接口");

        $uid    = $this->_post("uid",0);
        $sort   = $this->_post("sort",0);
        $number = $this->_post("number",10);

        $cacheKey = 'productsearchwords-'.$uid.'-'.$sort.'-'.$number;
        $this->checkCache($cacheKey,1,1);

        $result = apiCall(SearchWordsApi::FIND_S,[$sort,$number,'id,word,cnt']);
        if(!$result['status']) $this->apiReturnErr($result['info']);
        $this->cache($cacheKey,$result['info'],600);
    }
    //妈妈赠 发布
    public function addGift(){
      $this->checkVersion(101);
      $notes = "应用".$this->client_id."，调用妈妈赠发布接口";
      addLog("Product/addGift",$_GET,$_POST,$notes);
      if(IS_POST){
        $uid      = $this ->_post('uid',0,'uid错误');
        $title    = $this ->_post("title",'','标题不得为空');
        $message  = $this ->_post("message",'','message不得为空');
        $cate_id  = $this ->_post("cate_id",0,'cate_id错误');
        $template = $this ->_post("template",0);
        $main_img = $this ->_post("main_img",'');
        $show_imgs= $this ->_post("show_imgs",'');
        // $country  = $this ->_post("country", '');
        // $province = $this ->_post("province",'','省份不得为空');
        // $city     = $this ->_post("city",'');
        // $address  = $this ->_post("address",'');
        $country = '中国';
        $zone_id = $this ->_post("zone_id",'','地区ID不得为空');
        $areas   = getZoneByCode($zone_id);
        //check template
        if(!in_array(intval($template), [0,1])) $this->apiReturnErr('未知template');
        //check cate_id
        $r = apiCall(CategoryApi::GET_INFO,[['id'=>$cate_id]]);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        if(empty($r['info'])) $this->apiReturnErr('cate_id错误');
        if(intval($r['info']['level'])<2)  $this->apiReturnErr('请在下级类目发布');
        //用户是否存在
        $r = apiCall(UcenterMemberApi::GET_INFO,[['id'=>$uid],false,'id,username']);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        if(empty($r['info'])) $this->apiReturnErr('用户不存在');
        $username = $r['info']['username'];
        $now  = time();
        //用户店铺是否存在
        $r = apiCall(StoreApi::GET_INFO,[['uid'=>$uid],false,'id']);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        if(empty($r['info'])){
            //添加用户店铺
            $map = [
                'uid'         =>$uid,
                'name'        =>$username.'的小店',
                'desc'        =>'此人很懒,没有描述此店',
                'create_time' =>$now,
                'update_time' =>$now,
                'notes'       =>'妈妈赠自动建店'
            ];

            $r = apiCall(StoreApi::ADD,[$map]);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            $store_id = intval($r['info']);
        }else{
            $store_id = intval($r['info']['id']);
        }
        $model = new \think\model\Adv();
        $model ->startTrans();
        //add to product
        $map = [
            'uid'          =>$uid,
            'name'         =>$title,
            'template_id'  =>$template,
            'loc_country'  =>$country,
            'loc_province' =>$areas['provinceid'],
            'loc_city'     =>$areas['cityid'],
            'loc_address'  =>$areas['areaid'],
            'cate_id'      =>$cate_id,
            'createtime'   =>$now,
            'updatetime'   =>$now,
            'onshelf'      =>1, //0=>审核,1=>上架
            'status'       =>1,
            'store_id'     =>$store_id,
            'detail'       =>$message,
        ];
        $r = apiCall(ProductApi::OLD_ADD,[$map]);
        if(!$r['status']){
            $model->rollback();
            $this ->apiReturnErr($r['info']);
        }
        $pid = intval($r['info']);
        //add product-show-img
        $show_img_type = 6016;
        if($show_imgs){
            $imgs = explode(',', $show_imgs);
            $main_img = $imgs[0];
            $maps = [];
            foreach ($imgs as $v) {
                $maps[] = [
                    'pid'         =>$pid,
                    'img_id'      =>$v,
                    'type'        =>$show_img_type,
                    'create_time' =>$now
                ];
            }
            $r = apiCall(ProductImageApi::ADD_ALL,[$maps]);
            if(!$r['status']){
                $model->rollback();
                $this->apiReturnErr($r['info']);
            }
        }
        //add product-main-imgs
        $main_img_type = 6015;
        if($main_img){
            $map = [
                'pid'         =>$pid,
                'img_id'      =>$main_img,
                'type'        =>$main_img_type,
                'create_time' =>$now
            ];
            $r = apiCall(ProductImageApi::ADD,[$map]);
            if(!$r['status']){
                $model->rollback();
                $this->apiReturnErr($r['info']);
            }
        }
        //add to product_attr
        $map = [
            'pid'       =>$pid,
            'is_second' =>1
        ];
        $r = apiCall(ProductAttrApi::ADD,[$map]);
        if(!$r['status']){
            $model->rollback();
            $this->apiReturnErr($r['info']);
        }
        $model ->commit();
        $this->apiReturnSuc('发布成功');
      }
    }

    //妈妈赠 评价
    public function commentGift(){
      $this->checkVersion(101);
      $notes = "应用".$this->client_id."，调用MMZ评价接口";
      addLog("Product/commentGift",$_GET,$_POST,$notes);
      if(IS_POST){
        $pid = $this->_post('pid',0,'pid缺失');
        // $author_id = $this->_post('author_id',0,'author_id缺失');
        $uid = $this->_post('uid',0,'uid缺失'); //接受者
        $content = $this->_post('content','','content缺失');
        $send    = $this->_post('send','','请选择赠送奖励');

        $score1  = $this->_post('score1','0');
        $score2  = $this->_post('score2','0');
        $score3  = $this->_post('score3','0');
        $attachments = $this->_post('attachments','');
        if($score1<0 || $score1>5) $score1 = 0;
        if($score2<0 || $score2>5) $score2 = 0;
        if($score3<0 || $score3>5) $score3 = 0;
        //
        $send = intval($send);
        if(!in_array($send,[100,200,300])) $this->apiReturnErr('奖励错误');
        //商品是否存在  - ,'uid'=>$author_id
        $r = apiCall(ProductApi::GET_INFO,[['id'=>$pid],false,'id,uid,name']);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        if(empty($r['info'])) $this->apiReturnErr('pid错误');
        $pname  = $r['info']['name'];
        $to_uid = $r['info']['uid']; //赠送者
        //商品是否评价过 及 发货过
        $r = apiCall(MmzWinnerPepoleApi::GET_INFO,[['goods_id'=>$pid]]);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        if(empty($r['info'])) $this->apiReturnErr('该商品并未送出');
        $win_info = $r['info'];
        if(intval($win_info['express_time']) <= 0) $this->apiReturnErr('该商品并未发货');
        if(intval($win_info['evaluate_time']) > 0) $this->apiReturnErr('该商品已完成评价');
        $now = time();
        $model = new \think\model\Adv();
        $model ->startTrans();
        //写入评价
        $entity = [
          'score1'           => $score1,
          'score2'           => $score2,
          'score3'           => $score3,
          'evaluate_time'    => $now,
          'evaluate_content' => $content,
        ];
        $r = apiCall(MmzWinnerPepoleApi::SAVE,[['uid'=>$uid,'goods_id'=>$pid],$entity]);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        //写入附件
        if($attachments){
            $ids = explode(',', $attachments);
            // trace('---ids---');
            // trace($ids);
            foreach ($ids as $v) {
                $map = [
                    'img_id'      => $v,
                    'create_time' => $now,
                    'goods_id'    => $pid,
                ];
                $r = apiCall(MmzImageApi::ADD,[$map]);
                if(!$r['status']){
                    $model ->rollback();
                    $this ->apiReturnErr($r['info']);
                }
            }
        }
        //奖励萌币
        $r = apiCall(MemberApi::RAISE_SCORE,[$to_uid,$send]);
        if(!$r['status']){
            $model->rollback();
            $this->apiReturnErr($r['info']);
        }
        $model ->commit();

        //收到并评价完一次赠送变成vip2
        $result = apiCall(UserGroupApi::GET_INFO, ['uid' => $uid]);
        if(!$result['status']){
            if($result['info']['group_id'] != UserGroup::SECOND_GROUP || $result['info']['active_status']!=1){
                apiCall(UserGroupApi::SAVE, ['uid' => $uid], ['group_id' => UserGroup::SECOND_GROUP, 'active_status' =>1, 'active_time' => NOW_TIME]);
            }
        }

        //推送信息
        $text = '您赠出的'.$pname.'已收货,收到'.$send.UserLog::SCORE.'的回赠';
        $entity = [
            'from_id' =>0,
            'title'   =>Message::MESSAGE_SYSTEM,
            'content' =>$text,
            'summary' =>$text,
            'extra'   =>'', //消息记录中的
        ];
        $after_open = ['type'=>'go_activity','param'=>Message::MESSAGE_SYSTEM_ACTIVITY,'extra'=>[]]; //推送参数
        $r = apiCall(MessageApi::PUSH_MESSAGE_WITH_TYPE,[Message::MESSAGE_SYSTEM,$entity,$to_uid,false,$after_open]);
        if(!$r['status']) $this->apiReturnSuc($r['info']);
        $this ->apiReturnSuc('评价成功');
      }
    }
    //妈妈赠 收藏- 分页
    public function queryFavGift(){
      $this->checkVersion($this->api_ver);
      $notes = "应用".$this->client_id."，调用MMZ收藏分页查询接口";
      addLog("Product/queryFavGift",$_GET,$_POST,$notes);
      if(IS_POST){
        $maxs = $this->_post('max_img',3);
        $maxs = max(intval($maxs),0);
        $type = 3; //mmz商品
        $uid       = $this -> _post('uid',0,'用户id缺失');
        $page_no   = $this -> _post('page_no',1);
        $page_size = $this -> _post('page_size',10);

        $page = [
          'curpage' =>$page_no,
          'size'    =>$page_size,
        ];
        $map = [
          'f.uid'  => $uid,
          'f.type' => $type,
          '_string'=>'p.status=1 and p.onshelf=1',
        ];

        $field = "f.id,f.create_time,f.favorite_id as pid,p.name,m.uid,m.head,m.nickname,p.cate_id,c.cate_name,p.loc_country,p.loc_province,p.loc_city,image.main_img,a.view_cnt,wp.win_time,wp.win_uid,wp.evaluate_time,wp.express_time";
        $result = apiCall(FavoritesApi::QUERY_GIFT_WITH_COUNT,[$map,$page,false,false,$field]);
        if($result['status']){
          //查询附件
          foreach ($result['info']['list'] as &$v) {
            $pid = $v['pid'];
            //取用户信息
            if(empty($v['main_img'])) $v['main_img'] = '';
            $v['create_time_show'] = getDateInfo(time()-$v['create_time'],$v['create_time']);
            //真送状态
            $r = apiCall(MmzWinnerPepoleApi::GET_INFO,[['goods_id'=>$pid]]);
            if(!$r['status']) $this->apiReturnSuc($r['info']);
            if(empty($r['info'])) $v['send_out'] = '0';
            else $v['send_out'] = '1';
            //applies
            $r = apiCall(MmzApplyPepoleApi::COUNT,[['goods_id'=>$pid]]);
            if(!$r['status']) $this->apiReturnSuc($r['info']);
            $v['applies'] = $r['info'];
            //收藏数
            $r = apiCall(FavoritesApi::COUNT,[['favorite_id'=>$pid,'uid'=>$v['uid'],'type'=>1],'id']);
            if(!$r['status']) $this->apiReturnSuc($r['info']);
            $v['favs'] = $r['info'];
            //查询商品图片
            $v['show_imgs'] = [];
            $map = ['pid'=>$v['pid'],'type'=>6016];
            if($maxs){
                $r   = apiCall(ProductImageApi::QUERY,[$map,['curpage'=>1,'size'=>$maxs],false,false,'img_id']);
                if(!$r['status']) $this->apiReturnErr($r['info']);
                // $v['show_imgs_bak'] = $r;
                // $i = 0;
                foreach ($r['info']['list'] as $vv) {
                    $v['show_imgs'][] = $vv['img_id'];
                    // $i++;
                    // if($i>=$maxs) break;
                }
            }
          }
          $this -> apiReturnSuc($result['info']);
        }else{
          $this -> apiReturnErr($result['info']);
        }

      }
    }
    // 妈妈赠 发货
    public function express(){
      $this->checkVersion($this->api_ver);
      $notes = "应用".$this->client_id."，调用MMZ发货接口";
      addLog("Product/express",$_GET,$_POST,$notes);
      if(IS_POST){
        $pid = $this->_post('pid',0,'pid缺失');
        $uid = $this->_post('uid',0,'uid缺失');
        $express_code = $this->_post('express_code','','express_code缺失');
        $express_no = $this->_post('express_no','','express_no缺失');
        $now = time();
        //查询product
        $r = apiCall(ProductApi::GET_INFO,[['id'=>$pid,'uid'=>$uid],false,'id']);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        if(empty($r['info'])) $this->apiReturnErr('pid或uid错误或不匹配');
        //查询中奖信息及 是否已发货
        $r = apiCall(MmzWinnerPepoleApi::GET_INFO,[['goods_id'=>$pid],false,'id,express_time']);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        if(empty($r['info'])) $this->apiReturnErr('此商品并未送出');
        if(intval($r['info']['express_time'])>0) $this->apiReturnErr('此商品已发货');
        $id = $r['info']['id'];
        //查询express公司信息
        $r = apiCall(DatatreeApi::GET_INFO,[['id'=>$express_code],false,'name,code']);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        if(empty($r['info'])) $this->apiReturnErr('express_code错误');
        //写入express
        $map = [
          'express_no'   => $express_no,
          'express_code' => $r['info']['code'],
          'express_name' => $r['info']['name'],
          'express_time' => $now,
        ];
        $r = apiCall(MmzWinnerPepoleApi::SAVE_BY_ID,[$id,$map]);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        $this->apiReturnSuc('发货成功');
      }
    }

    /**
     * 用户的真无线 评价分页
     * @return [type] [description]
     */
    // public function commentGiftQuery(){
    //   $this->checkVersion($this->api_ver);
    //   $notes = "应用".$this->client_id."，调用二手商品评论分页查询接口";
    //   addLog("Product/commentGiftQuery",$_GET,$_POST,$notes);
    //   if(IS_POST){
    //     $uid      = $this ->_post('uid',0);
    //     $pageNo   = $this ->_post("page_no",1);
    //     $pageSize = $this ->_post("page_size",10);
    //     $max_img  = $this ->_post("max_img",3);
    //     //TODO cache
    //     $pager  = ['curpage'=>$pageNo,'size'=>$pageSize];
    //     $order  = 'evaluate_time desc';
        // $fields = 'id,user_id,score,comment,product_id,order_code';
        // $map    = ['uid' = $uid];
        // $result = apiCall(OrderCommentApi::QUERY_WITH_COUNT,[$map,$pager,$order]);
        // if($result['status']){
        //     $count = $result['info']['count'];
        //     $list  = $result['info']['list'];
        //     foreach ($list as &$v) {
        //         // order_code id product_id user_id
        //         // 查询规格
        //         $r = apiCall(OrdersItemApi::GET_INFO,[['p_id'=>$v['product_id'],'order_code'=>$v['order_code']]]);
        //         $info = $r['info'];
        //         if(!$r['status']) $this->apiReturnErr($info);
        //         if(empty($info)){
        //             $v['sku_info'] = [];
        //         }else{
        //             $v['sku_info'] = [
        //                 'id'         => $info['id'],
        //                 'name'       => $info['name'],
        //                 'img'        => $info['img'],
        //                 'sku_id'     => $info['sku_id'],
        //                 'sku_desc'   => $info['sku_desc'],
        //                 'count'      => $info['count'],
        //                 'createtime' => $info['createtime'],
        //             ];
        //         }
        //         //查询附件
        //         $this->getAttachs($v['id'],$max_img,$v);
        //         //查询用户
        //         $userInfo = $this->getUserInfo($v['user_id']);
        //         // trace($userInfo);
        //         $v['user_head'] = empty($userInfo) ? '' : $userInfo['head'];
        //         $v['user_nick'] = empty($userInfo) ? '' : $userInfo['nickname'];
        //         unset($userInfo);
        //     }
        //     $this->apiReturnSuc(['count'=>$count,'list'=>$list]);
        // }else{
        //     $this->apiReturnErr($result['info']);
        // }
    //   }
    // }
    /**
     * 商品评论分页查询
     * pageNo:当前页
     * pageSize:显示个数
     * pid商品ID
     */
    public function comment(){
      $this->checkVersion($this->api_ver);
      $notes = "应用".$this->client_id."，调用商品评论分页查询接口";
      addLog("Product/comment",$_GET,$_POST,$notes);
      if(IS_POST){
        $pid      = $this ->_post('pid',0);
        $pageNo   = $this ->_post("page_no",1);
        $pageSize = $this ->_post("page_size",10);
        $max_img  = $this ->_post("max_img",3);

        $cacheKey = 'productcomment-'.$pid.'-'.$pageNo.'-'.$pageSize.'-'.$max_img;
        $this->checkCache($cacheKey,$pageNo,5);

        $pager = ['curpage'=>$pageNo,'size'=>$pageSize];
        $order = 'createtime desc';
        // $fields = 'id,user_id,score,comment,product_id,order_code';
        $map   = [];
        $map['product_id'] = $pid;
        $map['status'] = 1;

        $result = apiCall(OrderCommentApi::QUERY_WITH_COUNT,[$map,$pager,$order]);
        if($result['status']){
            $count = $result['info']['count'];
            $list  = $result['info']['list'];
            foreach ($list as &$v) {
                // order_code id product_id user_id
                // 查询规格
                $r = apiCall(OrdersItemApi::GET_INFO,[['p_id'=>$v['product_id'],'order_code'=>$v['order_code']]]);
                $info = $r['info'];
                if(!$r['status']) $this->apiReturnErr($info);
                if(empty($info)){
                    $v['sku_info'] = [];
                }else{
                    $v['sku_info'] = [
                        'id'         => $info['id'],
                        'name'       => $info['name'],
                        'img'        => $info['img'],
                        'sku_id'     => $info['sku_id'],
                        'sku_desc'   => $info['sku_desc'],
                        'count'      => $info['count'],
                        'createtime' => $info['createtime'],
                    ];
                }
                //查询附件
                $this->getAttachs($v['id'],$max_img,$v);
                //查询用户
                $userInfo = $this->getUserInfo($v['user_id']);
                // trace($userInfo);
                $v['user_head'] = empty($userInfo) ? '' : $userInfo['head'];
                $v['user_nick'] = empty($userInfo) ? '' : $userInfo['nickname'];
                $v['user_sex']  = empty($userInfo) ? '' : $userInfo['sex'];
                unset($userInfo);
            }
            $this->cache($cacheKey,['count'=>$count,'list'=>$list]);
        }else{
            $this->apiReturnErr($result['info']);
        }
      }
    }
    /**
     * 二手商品分类 和 二手搜索 - 分页
     * name:    名称（模糊查询）(选填)
     * pageNo:  当前页
     * pageSize:显示个数
     * cate_id  商品分类ID(选填)
     */
    public function listGift(){
      $this->checkVersion($this->api_ver);
      $notes = "应用".$this->client_id."，调用二手商品分页查询接口";
      addLog("Product/listGift",$_GET,$_POST,$notes);
      if(IS_POST){
        $name     = $this ->_post('name',0);
        $zone_id  = $this ->_post("zone_id",'');
        $uid      = $this ->_post("uid",'');
        $to_uid   = $this ->_post("to_uid",'');
        $cate_id  = $this ->_post("cate_id",0);
        $order    = $this ->_post('order',Product::SORT_TIME_DESC);
        $pageNo   = $this ->_post("page_no",1);
        $fresh    = $this ->_post("fresh",1);
        $pageSize = $this ->_post("page_size",10);

        $cacheKey = 'productgift-'.$zone_id.'-'.$uid.'-'.$to_uid.'-'.$cate_id.'-'.$order.'-'.$pageNo.'-'.$pageSize.'-'.$name;
        $this->checkCache($cacheKey,$pageNo,5,$fresh);

        $now = time();
        $this -> cate = [];
        // $param = ['order'=>$order];
        //TODO 缓存
        $map = [
           'attr.is_second' => 1,
           'pro.onshelf'    => 1,//已审核
           'pro.status'     => 1,   //状态正常
           '_string'        =>'',
        ];
        if($cate_id != 0){
           // $param['cate_id'] = $cate_id;
           $this -> queryChild($cate_id);
           $map['_string'] .= ($map['_string'] ? ' and ' : '').'pro.`cate_id` in ('.implode(',', $this->cate).')';
        }
        if(!empty($name)){
           // $param['name'] = $name;
           $map['_string'] .= ($map['_string'] ? ' and ' : '').'pro.`name` like \'%'.$name.'%\'';

        }
        if(!empty($uid)){
           // $param['uid'] = $uid;
           $map['_string'] .= ($map['_string'] ? ' and ' : '').'pro.`uid` = '.$uid;
        }
        if(!empty($to_uid)){
           // $param['to_uid'] = $to_uid;
           $map['_string'] .= ($map['_string'] ? ' and ' : '').'wp.`uid` = '.$to_uid;

        }
        if(!empty($zone_id)){
           $map['_string'] .= ($map['_string'] ? ' and ' : '').'(pro.`loc_province`= \''.$zone_id.'\' or pro.`loc_city`=\''.$zone_id.'\' or pro.`loc_address` = \''.$zone_id.'\') ';
        }
        if($order== Product::SORT_VIEW_DESC){
            $order = 'attr.view_cnt desc';
        }else if($order== Product::SORT_TIME_DESC){
            $order = 'pro.createtime desc';
        // }else if($order== Product::SORT_NEW_30){
            // $order = false;
            // $time = time() - 30*24*3600;
            // $map['_string'] .= ($map['_string'] ? ' and ' : '').'`createtime` >='.$time;
        // }else if($order == Product::SORT_COMPREHENSIVE){
        //     $order = false;
        }else{
            $order = false;
        }
        $page   = ['curpage' => $pageNo,'size' => $pageSize];//分页产品
        $field  = "pro.id,pro.name,cate_id,c.name as cate_name,image.img_id as main_img,view_cnt,store_id,template_id,pro.uid,m.nickname,m.head,m.sex,loc_province,loc_city,loc_address,detail,pro.createtime,pro.updatetime,onshelf,win_time,wp.uid as win_uid,evaluate_time,express_time";
        if('' == $map['_string']) unset($map['_string']);
        $result = apiCall(ProductApi::QUERY_MMZ_COUNT,[$map,$page,$order,false,$field]);
        if($result['status']){
            $count = $result['info']['count'];
            $list  = $result['info']['list'];
            foreach ($list as &$v) {
                $pid = $v['id'];
                if(empty($v['main_img']))  $v['main_img'] = '';
                if(empty($v['cate_name'])) $v['cate_name'] = '';
                // if(empty($v['win_time']))  $v['win_time'] = '0';
                // if(empty($v['evaluate_time'])) $v['evaluate_time'] = '0';
                // if(empty($v['express_time']))  $v['express_time'] = '0';
                //时间跨度
                // $v['createtime_show'] = getDateInfo($now-$v['createtime'],$v['createtime']);
                //真送状态
                $r = apiCall(MmzWinnerPepoleApi::GET_INFO,[['goods_id'=>$pid]]);
                if(!$r['status']) $this->apiReturnErr($r['info']);
                if(empty($r['info'])) $v['send_out'] = '0';
                else $v['send_out'] = '1';
                //applies
                $r = apiCall(MmzApplyPepoleApi::COUNT,[['goods_id'=>$pid]]);
                if(!$r['status']) $this->apiReturnErr($r['info']);
                $v['applies'] = $r['info'];
                //收藏数
                $r = apiCall(FavoritesApi::COUNT,[['favorite_id'=>$pid,'uid'=>$v['uid'],'type'=>1],'id']);
                if(!$r['status']) $this->apiReturnErr($r['info']);
                $v['favs'] = $r['info'];
                //查询商品图片
                $v['show_imgs'] = [];
                $r = apiCall(ProductImageApi::QUERY_NO_PAGING,[['type'=>6016,'pid'=>$pid],false,'img_id']);
                if(!$r['status']) $this->apiReturnErr($r['info']);
                foreach ($r['info'] as $vv) {
                    $v['show_imgs'][] = $vv['img_id'];
                }
           }
           $this->cache($cacheKey,['count'=>$count,'list'=>$list],10);
        }else{
           $this->apiReturnErr("暂无商品");
        }
      }
    }
    /**
     * 商品分类 和 搜索 - 分页 - 不涉及会员价
     * name:    名称（模糊查询）(选填)
     * pageNo:  当前页
     * pageSize:显示个数
     * cate_id  商品分类ID(选填)
     */
    public function query(){
      $this->checkVersion($this->api_ver);
      $notes = "应用".$this->client_id."，调用商品分页查询接口";
      addLog("Product/query",$_GET,$_POST,$notes);
      if(IS_POST){

        // $cate_parent = $this ->_post("cate_parent",0);
        $name        = $this ->_post('name','');
        $cate_id     = $this ->_post("cate_id",'');
        $zone_id     = $this ->_post("zone_id",'');
        $service_id  = $this ->_post("service_id",'');
        $origin_country = $this ->_post("origin_country",'');
        $special     = $this ->_post("special",0);
        $pageNo      = $this ->_post("page_no",1);
        $pageSize    = $this ->_post("page_size",10);
        $order       = $this ->_post('order',Product::SORT_COMPREHENSIVE);
        $min_cost    = $this ->_post('min_cost',0.0);
        $max_cost    = $this ->_post('max_cost',0.0);
        $fresh       = $this ->_post('fresh',0);
        $name        = trim($name);

        // $cacheKey = 'productquery-'.$zone_id.'-'.$cate_id.'-'.$service_id.'-'.$origin_country.'-'.$special.'-'.$order.'-'.$pageNo.'-'.$pageSize.'-'.$name.'-'.$min_cost.'-'.$max_cost;
        // $this->checkCache($cacheKey,$pageNo,5,$fresh);

        $this -> cate = [];
        // $param = [];
        // $param['name']      = $name;
        // $param['cate_id']   = $cate_id;
        // // $param['page_size'] = $pageSize;
        // $param['order']     = $order;
        // $param['min_cost']  = $min_cost;
        // $param['max_cost']  = $max_cost;
        //TODO 缓存
        $map = [
            'attr.is_second' => 0,
            'pro.onshelf'   => 1,   //已上架
            'pro.status'    => 1,   //状态正常
            '_string'   =>'',
        ];
        if($cate_id){
            $this -> queryChild($cate_id);
            $map['_string'] .= ($map['_string'] ? ' and ' : ' ').'`pro`.`cate_id` in ('.implode(',', $this->cate).')';
        }
        if($name){
           $map['_string'] .= ($map['_string'] ? ' and ' : ' ').'`pro`.`name` like \'%'.$name.'%\'';
           //添加搜索词记录
           $r = apiCall(SearchWordsApi::ADD_ONE,[$name]);
           if(!$r['status']) $this->apiReturnErr($r['info']);
        }
        if($service_id){
            $map['service_ids'] = array_unique(explode(',', $service_id));
        }
        if($origin_country){
            $map['_string'] .= ($map['_string'] ? ' and ' : ' ').'`dt_origin_country` = '.$origin_country;
        }
        if($zone_id){
           $map['_string'] .= ($map['_string'] ? ' and ' : ' ').'(`loc_province`= \''.$zone_id.'\' or `loc_city`=\''.$zone_id.'\' or `loc_address` = \''.$zone_id.'\') ';
        }
        if($max_cost != 0.0){
           $map['_string'] .= ($map['_string'] ? ' and ' : ' ').'`price` <='.$max_cost;
        }
        if($min_cost != 0.0){
           $map['_string'] .= ($map['_string'] ? ' and ' : ' ').'`price` >='.$min_cost;
        }
        $special = intval($special);
        if($special === 1){
            //TODO 保税区 - 移到web
            //get保税区ids
            // $map = [
            //     'propname'=>'保税区',
            //     '_string'=>'`cate_id` in ('.implode(',', $this->cate).')',
            // ];
        }elseif($special === 2){
            //限量购 - 移到web
            $map['_string'] .= ($map['_string'] ? ' and ' : ' ').' `attr`.`buy_limit` > 0 ';
        }
        if($order == Product::SORT_COMPREHENSIVE){
            $order = false;
        }else if($order == Product::SORT_PRICE_ASC){
            $order = "price asc";
        }else if($order == Product::SORT_PRICE_DESC){
            $order = "price desc";
        }else if($order == Product::SORT_SALE){
            $order = "attr.total_sales desc";
        }else if($order== Product::SORT_VIEW_DESC){
            $order = 'view_cnt desc';
        }else if($order== Product::SORT_TIME_DESC){
            $order = 'pro.createtime desc';
        // }else if($order== Product::SORT_NEW_30){
            // $order = false;
            // $time = time() - 30*24*3600;
            // $map['_string'] .= ($map['_string'] ? ' and ' : '').'`createtime` >='.$time;
        }else{
            $order = false;
        }

        $page   = ['curpage' => $pageNo,'size' => $pageSize]; //分页产品
        $field  = "pro.id,pro.name,total_sales,cate_id,loc_province,loc_city,loc_address,dt_origin_country,image.img_id as img,price,view_cnt,buy_limit,store_id,template_id,ori_price,is_second,discount";
        if(empty($map['_string'])) unset($map['_string']);
        $r = apiCall(ProductApi::QUERY_WITH_COUNT,[$map,$page,$order,false,$field]);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        if(empty($r['info'])) $this->apiReturnSuc([]);
        $count = $r['info']['count'];
        $list  = $r['info']['list'];
        foreach ($list as &$v) {
            $pid = $v['id'];
            $now = time();
            $store_id = $v['store_id'];
            unset($v['is_second']);
            //获取商品最低价(包括会员价和规格价)
            $res = (new ProductSkuApi())->getMinPriceByPid($pid);
            if(!$res['status']) $this->apiReturnErr($r['info']);
            $v['min_price'] = $res['info'];
            //店铺优惠信息
            $result = apiCall(StorePromotionApi::GET_INFO,[['store_id'=>$store_id,'_string'=>'`start_time`<'.$now.' and `end_time`>'.$now]]);
            if(!$result['status']) $this->apiReturnErr($result['info']);
            $v['store_benefit'] = $result['info'];
            //获取运费信息
            // $result = apiCall(FreightTemplateApi::GET_FREIGHT_DES,[$v['template_id']]);
            // $arr = [];
            // if(!$result['status']) $arr['err'] = $result['info'];
            // else $v['freight_info']['suc'] = $result['info'];
            //获取属性 - 保税区
            // $result = apiCall(ProductPropApi::GET_PROP2,[$pid]);
            // if(!$result['status']) $this->apiReturnErr($result['info']);
            // trace('---productProp---');
            // trace($result['info']);
            // $v['rate_keep_zone'] = '';
            // $v['born']         = '';
            // foreach ($result['info'] as $vv) {
            //     if($vv['name'] =='保税区') $v['rateKeepZone'] = $vv['value'];
            //     // if($vv['name'] =='产地') $v['born'] = $vv['value'];
            // }
            // if(!empty($result['info'])){
            //     // if(isset($result['info']['']))
            // }
            //获取分组信息 - id
            $result = apiCall(ProductGroupApi::QUERY_NO_PAGING,[['p_id'=>$pid,'_string'=>'`start_time`<'.$now.' and `end_time`>'.$now],false,'g_id,p_id,sku_id,price,start_time,end_time']);
            if(!$result['status']) $this->apiReturnErr($result['info']);
            $v['group_info'] = $result['info'];
            //获取服务信息 - id
            $result = apiCall(ProductServiceApi::QUERY_NO_PAGING,[['p_id'=>$pid],false,'dt_service_id as id']);
            if(!$result['status']) $this->apiReturnErr($result['info']);
            $services = [];
            foreach ($result['info'] as $vv) {
                $services[] = $vv['id'];
            }
            $v['service_info'] = array_unique($services);
            unset($result);unset($services);
        }
        // $this->cache($cacheKey,['count'=>$count,'list'=>$list]);
        $this->apiReturnSuc(['count'=>$count,'list'=>$list]);
      }
    }
    //商品分组分页查询
    public function queryByGroup(){
        $this->checkVersion($this->api_ver);
        $notes = "应用" . $this->client_id ."调用商品按分组查询接口接口";
        addLog("Product/queryByGroup", $_GET, $_POST, $notes);

        if(IS_POST){

            $group_id  = $this -> _post('group_id',0,'商品分组id缺失');
            $cate_id   = $this -> _post('cate_id',0);
            $page_no   = $this -> _post('page_no',1);
            $page_size = $this -> _post('page_size',10);
            $order     = $this -> _post('order',Product::SORT_COMPREHENSIVE);
            $fresh     = $this -> _post('fresh',0);

            $cacheKey = 'productgroup-'.$group_id.'-'.$cate_id.'-'.$order.'-'.$page_no.'-'.$page_size;
            $this->checkCache($cacheKey,$page_no,5,$fresh);

            // $param = [];
            // $param['group_id']  = $group_id;
            // $param['cate_id']   = $cate_id;
            // $param['order']     = $order;
            $map = [
                'g_id'    => $group_id,
                'status'  => 1,//未删除
                'onshelf' => 1,//已上架
            ];
            //时间限制
            $map['_string'] = '`start_time`<='.time().' AND `end_time`>='.time();
            //下面在tp5rc2 bug : whereparse error
            // $map['start_time'] = ['elt',time()];
            // $map['end_time']   = ['egt',time()];
            if($cate_id != 0){
                $this -> cate =[];
                $this->queryChild($cate_id);
                // $res = $this->queryChild($cate_id);
                // if(!$res['status']) $this -> apiReturnErr($res['info']);
                $map['_string'] = ' and `cate_id` in('.implode(',', $this->cate).')';
            }

            if($order == Product::SORT_COMPREHENSIVE){
                $order = false;
            }else if($order == Product::SORT_START_TIME_ASC){
                $order = "start_time asc";
            }else if($order == Product::SORT_START_TIME_DESC){
                $order = "start_time desc";
            }else if($order == Product::SORT_END_TIME_ASC){
                $order = "end_time asc";
            }else if($order == Product::SORT_END_TIME_DESC){
                $order = "end_time desc";
            }

            $page = ['curpage'=>$page_no,'size'=>$page_size]; //分页产品
            $field = "p_id as id,p.name,total_sales,cate_id,image.img_id as img,min(price) as price,sku.ori_price,dt.code as origin_country,dt.name as origin_country_name";

            $result = apiCall(ProductGroupApi::QUERY_WITH_PRODUCT,[$map,$page,$order,false,$field]);

            if($result['status']){
                //查询分组信息 - 最大结束时间 - 主图 - name
                $r = apiCall(DatatreeApi::GET_INFO,[['id'=>$group_id],false,'id,name,iconurl']);
                if(!$r['status']) $this->apiReturnErr($r['info']);
                $arr = [];
                $arr['id']   = $r['info']['id'];
                $arr['name'] = $r['info']['name'];
                $arr['img']  = $r['info']['iconurl'];
                $r = apiCall(ProductGroupApi::GET_END_TIME,[$group_id]);
                if(!$r['status']) $this->apiReturnErr($r['info']);
                $arr['end_time'] = isset($r['info']['end_time']) ? $r['info']['end_time'] : '';
                $r = array_merge($arr,$result['info']);
                $this -> cache($cacheKey,$r);
            }else{
                $this -> apiReturnErr($result['info']);
            }

        }

    }
    //商品咨询详情
    public function detailFaq(){
        $this->checkVersion($this->api_ver);
        $notes = "应用" . $this->client_id ."调用查询商品咨询详情接口";
        addLog("Product/detailFaq", $_GET, $_POST, $notes);
        if(IS_POST){
            $id     = $this -> _post('id','','咨询id不能为空');
            $result = apiCall(ProductFaqApi::GET_INFO,[['id'=>$id]]);
            if(!$result['status']){
                $this->apiReturnErr($result['info']);
            }else{
                if(!empty($result['info'])){
                    unset($result['info']['ask_username']);//废弃
                    unset($result['info']['reply_username']);//废弃
                    //查询用户信息
                    $uid = $result['info']['ask_uid'];
                    $userInfo = $this->getUserInfo($uid);
                    $result['info']['ask_nickname'] =  empty($userInfo) ? '' :$userInfo['nickname'];
                    $result['info']['ask_head']     =  empty($userInfo) ? '' :$userInfo['head'];
                    $result['info']['ask_sex']     =  empty($userInfo) ? '' :$userInfo['sex'];
                    $uid = intval($result['info']['reply_uid']);
                    if($uid){
                        $userInfo = $this->getUserInfo($uid);
                        $result['info']['reply_nickname'] =  empty($userInfo) ? '' :$userInfo['nickname'];
                        $result['info']['reply_head']     =  empty($userInfo) ? '' :$userInfo['head'];
                        $result['info']['reply_sex']     =  empty($userInfo) ? '' :$userInfo['sex'];
                    }else{
                        $result['info']['reply_nickname'] = '';
                        $result['info']['reply_head']     = '';
                        $result['info']['reply_sex']     = '';
                    }

                }
                $this->apiReturnSuc($result['info']);
            }
        }
    }

    //商品咨询查询
    public function queryFaq(){
        $this->checkVersion($this->api_ver);
        $notes = "应用" . $this->client_id ."调用查询商品咨询接口";
        addLog("Product/queryFaq", $_GET, $_POST, $notes);

        if(IS_POST){
            $pid       = $this -> _post('pid','','pid不能为空');
            $page_no   = $this -> _post('page_no',1);
            $page_size = $this -> _post('page_size',10);
            $fresh     = $this -> _post('fresh',0);

            $cacheKey = 'productfaq-'.$pid.'-'.$page_no.'-'.$page_size;
            $this->checkCache($cacheKey,$page_no,5,$fresh);

            $map   = ['pid' => $pid];
            $page  = ['curpage'=>$page_no,'size'=>$page_size];
            $order = "ask_time desc,reply_time desc";

            // dump($map);exit;
            $result = apiCall(ProductFaqApi::QUERY_WITH_COUNT,[$map,$page,$order,false,'id,ask_uid,ask_time,ask_content,reply_uid,reply_time,reply_content']);
            if($result['status']){
                foreach($result['info']['list'] as &$vo){
                    //提问者信息
                    $userInfo = $this->getUserInfo($vo['ask_uid']);
                    $vo['ask_nickname'] = empty($userInfo) ? '' : $userInfo['nickname'];
                    $vo['ask_head']     = empty($userInfo) ? '' : $userInfo['head'];
                    $vo['ask_sex']      = empty($userInfo) ? '' : $userInfo['sex'];
                    //回复者信息
                    if($vo['reply_uid']){
                        $userInfo = $this->getUserInfo($vo['reply_uid']);
                        $vo['reply_nickname'] = empty($userInfo) ? '' : $userInfo['nickname'];
                        $vo['reply_head']     = empty($userInfo) ? '' : $userInfo['head'];
                        $vo['reply_sex']      = empty($userInfo) ? '' : $userInfo['sex'];
                    }else{
                        $vo['reply_nickname'] = '';
                        $vo['reply_head']     = '';
                        $vo['reply_sex']      = '';
                    }
                }
                unset($userInfo);
                $this -> cache($cacheKey,$result['info']);
            }else{
                $this -> apiReturnErr($result['info']);
            }

        }

    }

    //咨询保存
    public function ask(){
        $this->checkVersion($this->api_ver);
        $notes = "应用" . $this->client_id ."调用商品咨询接口";
        addLog("Product/ask", $_GET, $_POST, $notes);
        if(IS_POST){
            $pid = $this -> _post('pid','','pid不能为空');
            $ask_content = $this -> _post('ask_content','','询问内容不能为空');
            $ask_uid = $this -> _post('ask_uid','','用户id不能为空');
            // $ask_username = $this -> _post('ask_username','','用户名不能为空');

            $map = [
                'id'       => $ask_uid,
                // 'username' => $ask_username,
            ];
            $this->getUserInfo($ask_uid);//apiCall(UserApi::FIND,[$map]);
            $entity = [
                'pid'            => $pid,
                'ask_content'    => $ask_content,
                'ask_uid'        => $ask_uid,
                'ask_username'   => '', //废弃
                'ask_time'       => time(),
                'reply_content'  => "",
                'reply_uid'      => 0,
                'reply_username' => "", //废弃
                'reply_time'     => 0,
            ];
            $result = apiCall(ProductFaqApi::ADD,[$entity]);
            if($result['status']){
                $this -> apiReturnSuc("资讯已提交！请耐心等待回复");
            }else{
                $this -> apiReturnErr($result['info']);
            }

        }

    }
    //收藏商品 - 分页
    public function getFavorites(){
        $this->checkVersion($this->api_ver);
        $notes = "应用" . $this->client_id ."调用分页查询收藏商品接口";
        addLog("Product/getFavorites", $_GET, $_POST, $notes);

        if(IS_POST){

            $type = 1; //商品
            $uid       = $this -> _post('uid',0,'用户id缺失');
            $page_no   = $this -> _post('page_no',1);
            $page_size = $this -> _post('page_size',10);

            $page = [
                'curpage' =>$page_no,
                'size'    =>$page_size,
            ];
            $map = [
                'itboye_favorites.uid'  => $uid,
                'itboye_favorites.type' => $type,
            ];

            $field = "product.id,name,total_sales,product.cate_id,img,price,ori_price";
            $result = apiCall(FavoritesApi::QUERY_PRODUCT_WITH_COUNT,[$map,$page,false,false,$field]);

            if($result['status']){
              $this -> apiReturnSuc($result['info']);
            }else{
              $this -> apiReturnErr($result['info']);
            }

        }

    }

    /**
     * 收藏、取消收藏
     */
    public function favorites(){
        $this->checkVersion(100);
        $notes = "应用" . $this->client_id ."调用收藏商品接口";
        addLog("Product/favorites", $_GET, $_POST, $notes);

        if(IS_POST){
            // $type = 1;
            $uid  = $this -> _post('uid',0,'用户id缺失!');
            $type = $this -> _post('fav_type',1);
            $favorite_id = $this -> _post('pid',0,'商品id缺失!');
            $result = apiCall(ProductApi::GET_INFO,[['id' => $favorite_id]]);
            $type = intval($type);
            if(!in_array($type,[1,3])) $this->apiReturnErr('type错误');
            if($result['status']){
                if(empty($result['info'])) $this->apiReturnErr('无此商品');
                if($result['info']['status']==-1 || $result['info']['onshelf']==0){
                    $this -> apiReturnErr('商品状态错误!');
                }
            }else{
                $this -> apiReturnErr($result['info']);
            }

            $favorite_value = $this -> _post('favorite_value',-1,'favorite_value参数缺失!');

            $map = [
                'uid'         => $uid,
                'favorite_id' => $favorite_id,
                'type'        => $type,
            ];

            $result = apiCall(FavoritesApi::GET_INFO,[$map]);
            if(!$result['status']){
                $this -> apiReturnErr($result['info']);
            }

            if(1 == $favorite_value){
                // trace('商品收藏 1');
                // trace($result);
                if($result['info']){
                    //已经存在收藏信息
                    $this -> apiReturnErr('已收藏过!');
                }
                $favorite_content = json_encode($this->getContent($favorite_id));//获取商品详情形成json
                $entity = [
                    'uid'              => $uid,
                    'type'             => $type,
                    'favorite_id'      => $favorite_id,
                    'favorite_content' => $favorite_content,
                    'create_time'      => time(),
                ];
                $result = apiCall(FavoritesApi::ADD,[$entity]);
                if($result['status']){
                    $this -> apiReturnSuc('收藏成功');//返回id
                }else{
                    $this -> apiReturnErr($result['info']);
                }

            }else if(0 == $favorite_value){
                // trace('取消收藏 0');
                // trace($result);
                if(empty($result['info'])){
                    //不存在收藏信息
                    $this -> apiReturnErr('未收藏过!');
                }
                // trace('product-domain fav');
                // trace($result);
                $id = $result['info']['id'];
                $map = [
                    'id' => $id,
                ];
                $result = apiCall(FavoritesApi::DELETE,[$map]);
                if($result['status']){
                    $this -> apiReturnSuc('取消收藏成功！');
                }else{
                    $this -> apiReturnErr($result['info']);
                }
            }else{
                $this -> apiReturnErr("favorite_value参数错误!");
            }

        }
    }
    //二手商品申请
    public function applyGift(){
        $this->checkVersion(100);
        $notes = "应用" . $this->client_id ."调用二手商品申请接口";
        addLog("Product/applyGift", $_GET, $_POST, $notes);

        if(IS_POST){
            $uid    = $this -> _post('uid',0,'uid缺失');
            $pid    = $this -> _post('pid',0,'pid不能为空');
            $reason = $this -> _post('reason','','不能无故申请');
            $aid    = $this -> _post('address_id',0,'address_id不得为空');
            //检查商品
            $r = apiCall(ProductApi::GET_INFO,[['id'=>$pid]]);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            if(empty($r['info'])) $this->apiReturnErr('无此商品');
            if(0 === intval($r['info']['onshelf'])) $this->apiReturnErr('该商品审核中...');
            if(-1 === intval($r['info']['status'])) $this->apiReturnErr('该商品已删除');
            if(intval($uid)==intval($r['info']['uid'])) $this->apiReturnErr('不能申请自己的商品');
            //检查用户
            $r = apiCall(MemberApi::GET_INFO,[['uid'=>$uid]]);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            if(empty($r['info'])) $this->apiReturnErr('错误用户');
            //检查收货地址
            $r = apiCall(AddressApi::GET_INFO,[['uid'=>$uid,'id'=>$aid]]);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            if(empty($r['info'])) $this->apiReturnErr('错误地址');
            //组装收货地址
            $linkman = $r['info']['contactname'];
            $mobile  = $r['info']['mobile'];
            $address = '';
            if(in_array($r['info']['country'],['中国','1017','86']))
                $address .= $r['info']['country'];
            $address .= $r['info']['province'].$r['info']['city'].' '.$r['info']['detailinfo'];
            //写入申请
            $map = [
                'uid'        =>$uid,
                'reason'     =>$reason,
                'apply_time' =>time(),
                'address'    =>$address,
                'goods_id'   =>$pid,
                'mobile'     =>$mobile,
                'linkman'    =>$linkman
            ];
            $r = apiCall(MmzApplyPepoleApi::ADD,[$map]);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            $this->apiReturnSuc('申请成功');
        }
    }
    //mmz 类目
    public function giftCate(){
        $this->checkVersion(100);
        $notes = "应用" . $this->client_id ."调用二手商品类目查询接口";
        addLog("Product/giftCate", $_GET, $_POST, $notes);

        if(IS_POST){
            $cacheKey = 'gitcate';
            $this->checkCache($cacheKey,1,1);
            $r = apiCall(CategoryApi::GET_ALL);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            $this->cache($cacheKey,$r['info'],600);
        }
    }

    //二手商品申请列表 - 分页
    public function applyList(){
        $this->checkVersion(100);
        $notes = "应用" . $this->client_id ."调用二手商品申请分页查询接口";
        addLog("Product/applyList", $_GET, $_POST, $notes);

        if(IS_POST){
            $id   = $this -> _post('pid',0,'商品id缺失');
            $page = $this -> _post('page_no',1);
            $size = $this -> _post('page_size',10);
            $fresh = $this -> _post('fresh',0);

            $cacheKey = 'productapply-'.$id.'-'.$page.'-'.$size;
            $this->checkCache($cacheKey,$page,5,$fresh);

            $map  = ['a.goods_id'=>$id];
            $r = apiCall(MmzApplyPepoleApi::QUERY_COUNT,[$map,['page'=>$page,'size'=>$size],false,'a.apply_time desc','a.id,a.uid,a.reason,a.apply_time,a.goods_id,m.head,m.nickname']);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            foreach ($r['info']['list'] as &$v) {
                $v['apply_time_show'] = getDateInfo(time()-intval($v['apply_time']),$v['apply_time']);
            }
            $this->cache($cacheKey,$r['info']);
        }
    }
    //二手商品 - 选定
    public function chose(){
        $this->checkVersion(100);
        $notes = "应用" . $this->client_id ."调用二手商品赠送接口";
        addLog("Product/chose", $_GET, $_POST, $notes);

        if(IS_POST){
            $pid = $this -> _post('pid',0,'pid缺失');
            $uid = $this -> _post('uid',0,'uid缺失');
            $aid = $this -> _post('apply_id','','apply_id缺失');
            //验证pid
            $map = ['id'=>$pid,'uid'=>$uid];
            $r = apiCall(ProductApi::GET_INFO,[$map,false,'id,onshelf,status']);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            if(empty($r['info'])) $this->apiReturnErr('uid或pid错误');
            if(0 === intval($r['info']['onshelf'])) $this->apiReturnErr('该商品审核中...');
            if(-1 === intval($r['info']['status'])) $this->apiReturnErr('该商品已删除...');
            //查询申请记录
            $r = apicall(MmzApplyPepoleApi::GET_INFO,[['id'=>$aid],false,'uid']);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            if(empty($r['info'])) $this->apiReturnErr('apply_id错误');
            $to_uid = $r['info']['uid'];
            //是否已送出
            $r = apiCall(MmzWinnerPepoleApi::COUNT,[['goods_id'=>$pid],'id']);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            if(1 === intval($r['info'])) $this->apiReturnErr('该商品已送出');
            //赠送记录
            $map = [
                'uid'      =>$to_uid,
                'win_time' =>time(),
                'apply_id' =>$aid,
                'goods_id' =>$pid,
            ];
            $r = apiCall(MmzWinnerPepoleApi::ADD,[$map]);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            $this->apiReturnSuc('选择成功');
        }
    }
    //二手商品详情
    public function detailGift(){
        $this->checkVersion($this->api_ver);
        $notes = "应用" . $this->client_id ."调用商品详情接口";
        addLog("Product/detail", $_GET, $_POST, $notes);

        if(IS_POST){
            $id   = $this -> _post('pid',0,'商品id缺失');
            $uid  = $this -> _post('uid',0);
            $size = $this -> _post('page_size',10);

            $r    = apiCall(ProductApi::DETAIL_GIFT,[$id,$uid]);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            if(empty($r['info'])) $this->apiReturnErr('商品id缺失');
            $r['info']['createtime_show'] = getDateInfo(time()-intval($r['info']['createtime']),$r['info']['createtime']);
            // unset($r);
            $info = $r['info'];
            unset($info['weight']);
            unset($info['synopsis']);
            unset($info['dt_origin_country']);
            unset($info['dt_goods_unit']);
            if(isset($info['pid'])) unset($info['pid']);
            if(empty($info['apply_id'])) $info['apply_id'] = '0';
            if(empty($info['main_img'])) $info['main_img'] = '';
            // 查看次数 +1
            apiCall(ProductAttrApi::SET_INC,[['pid'=>$id],'view_cnt',1]);
            //查询show_imgs
            $info['show_imgs'] = [];
            $r = apiCall(ProductImageApi::QUERY_NO_PAGING,[['pid'=>$id,'type'=>6016],false,'img_id']);
            if(!$r['status']) $this->apiReturnErr($r['info']);
            foreach ($r['info'] as $v) {
                $info['show_imgs'][] = $v['img_id'];
            }
            //赠送状态
            $r = apiCall(MmzWinnerPepoleApi::GET_INFO2,[['w.goods_id'=>$id],false,'w.uid,w.win_time,w.express_time,w.evaluate_time,w.apply_id,a.linkman,a.mobile,a.address,a.reason']);
            // ,a.mobile,a.linkman,a.address
            if(!$r['status']) $this->apiReturnErr($r['info']);
            $info['send_info'] = $r['info'];
            if($info['send_info']){
                //受赠用户信息
                $userInfo = $this->getUserInfo($info['send_info']['uid']);
                $info['send_info']['head']     = $userInfo['head'];
                $info['send_info']['nickname'] = $userInfo['nickname'];
                $info['send_info']['sex']      = $userInfo['sex'];
                unset($userInfo);
            }else{
                $info['send_info'] = ['temp'=>''];
            }

            //是否收藏
            $info['is_fav'] = '0';
            if($uid){
                $r = apiCall(FavoritesApi::GET_INFO,[['uid'=>$uid,'type'=>3,'favorite_id'=>$id],false,'id']);
                if(!$r['status']) $this->apiReturnErr($r['info']);
                if(!empty($r['info'])) $info['is_fav'] = '1';
            }

            //查询申请列表 - 第一页
            $map = ['a.goods_id'=>$id];
            $r = apiCall(MmzApplyPepoleApi::QUERY_COUNT,[$map,['page'=>1,'size'=>$size],false,'a.apply_time desc','a.id,a.uid,a.reason,a.apply_time,a.goods_id,m.head,m.nickname']);
            //,a.mobile,a.linkman,a.address
            if(!$r['status']) $this->apiReturnErr($r['info']);
            foreach ($r['info']['list'] as &$v) {
                $v['apply_time_show'] = getDateInfo(time()-intval($v['apply_time']),$v['apply_time']);
            }
            $info['apply_list'] = $r['info'];
            $this->apiReturnSuc($info);
        }
    }
    //商品详情
    public function detail(){
        $this->checkVersion($this->api_ver);
        $notes = "应用" . $this->client_id ."调用商品详情接口";
        addLog("Product/detail", $_GET, $_POST, $notes);

        if(IS_POST){
            $id  = $this -> _post('pid',0,'商品id缺失');
            $uid = $this -> _post('uid',0);
            $gid = $this -> _post('gid',0);
            $result = apiCall(ProductApi::DETAIL,[$id]);
            if(!$result['status']){
                $this -> apiReturnErr($result['info']);
            }else{
                // 查看次数 +1
                apiCall(ProductAttrApi::SET_INC,[['pid'=>$id],'view_cnt',1]);
                // $this -> apiReturnSuc($result['info']);
                // trace('info...');
                // trace($result['info']);
                if( -1 == $result['info']['status']){
                    $this -> apiReturnErr('该商品已删除');
                }elseif(0 == $result['info']['onshelf']){
                    $this -> apiReturnErr('该商品已下架');
                }
                if(empty($result['info']['origin_country_name'])) $result['info']['origin_country_name'] = '';

                $c = $result['info']['origin_country'];
                $result['info']['origin_country_img'] = $c ? getCdnIcon('country',60,'by'.$c):'';
                // trace('==info==');
                // trace($result['info']);
                //清除不用字段
                unset($result['info']['uid']);
                // unset($result['info']['loc_country']);
                // unset($result['info']['loc_province']);
                // unset($result['info']['loc_city']);
                // unset($result['info']['loc_address']);
                unset($result['info']['createtime']);
                unset($result['info']['updatetime']);
                unset($result['info']['onshelf']);
                unset($result['info']['status']);
                unset($result['info']['weight']);
                // unset($result['info']['synopsis']);
                unset($result['info']['dt_origin_country']);
                unset($result['info']['_price']);
                unset($result['info']['dt_goods_unit']);
                unset($result['info']['pid']);
                unset($result['info']['detail']);

                $now = time();
                $pid = $id;
                $store_id = $result['info']['store_id'];
                 //店铺优惠信息
                $res = apiCall(StorePromotionApi::GET_INFO,[['store_id'=>$store_id,'_string'=>'`start_time`<'.$now.' and `end_time`>'.$now]]);
                if(!$res['status']) $this->apiReturnErr($res['info']);
                $result['info']['store_benefit'] = empty($res['info']) ? ['temp'=>'']:$res['info'];
                $result['info']['gid'] = ''.$gid;

                //获取运费信息
                // $res = apiCall(FreightTemplateApi::GET_FREIGHT_DES,[$result['info']['template_id']]);
                // $arr = ['msg'=>'','data'=>[]];
                // if(!$res['status']) $arr['msg'] = $res['info'];
                // else $arr['data'] = $res['info'];
                // $result['info']['freight_info'] = $arr;
                //获取属性 - 产地 - 保税区
                // $res = apiCall(ProductPropApi::GET_PROP2,[$pid]);
                // if(!$res['status']) $this->apiReturnErr($res['info']);
                //$result['info'];
                // trace('---productProp---');
                // trace($res['info']);
                // $result['info']['rate_keep_zone'] = '';
                // $result['info']['born']   = '';
                // foreach ($res['info'] as $v) {
                //     if($v['name'] =='保税区') $result['info']['rate_keep_zone'] = $v['value'];
                //     // if($v['name'] =='产地') $result['info']['born'] = $v['value'];
                // }
                //获取服务信息
                $res = apiCall(ProductServiceApi::QUERY_NO_PAGING,[['p_id'=>$pid],false,'dt_service_id as id']);
                if(!$res['status']) $this->apiReturnErr($res['info']);
                $services = [];
                foreach ($res['info'] as $v) {
                    $services[] = $v['id'];
                }
                $result['info']['service_info'] = array_unique($services);
                unset($res);unset($services);

                //获取分组信息 - id
                $res = apiCall(ProductGroupApi::QUERY_NO_PAGING,[['p_id'=>$pid,'_string'=>'`start_time`<'.$now.' and `end_time`>'.$now],false,'g_id,p_id,sku_id,price,start_time,end_time']);
                if(!$res['status']) $this->apiReturnErr($res['info']);
                $skus = [];
                foreach ($res['info'] as &$v) {
                    $gid = intval($v['g_id']);
                    if(!isset($skus[$gid])) $skus[$gid] = ['gid'=>$v['g_id']];
                    $skus[$gid]['sku_info'][] = ['sku_id'=>$v['sku_id'],'price'=>$v['price'],'start_time'=>$v['start_time'],'end_time'=>$v['end_time']];
                }
                $result['info']['group_info'] = array_values($skus);
                unset($skus);

                // $has_sku = apiCall(ProductSkuApi::HAS_SKU,[$id]);
                // if($has_sku['status']){
                //     //0为统一规格,1为多规格
                //     $has_sku = $has_sku['info'];
                // }else{
                //     $this -> apiReturnErr($has_sku['info']);
                // }
                // favs ;is_fav;remain
                $has_sku = intval($result['info']['has_sku']);
                $count   = 0; //商品总数


                // = $has_sku;
                if(1 === $has_sku){
                    //多规格传值
                    //规格id
                    $skuId = apiCall(ProductSkuApi::GET_SKU_ID,[$id,'sku_id']);
                    // trace('==skuId==');
                    // trace($skuId);
                    if($skuId['status']){
                        $result['info']['sku_id'] = json_decode($skuId['info']);
                    }else{
                        $this -> apiReturnErr($skuId['info']);
                    }
                    //规格名称
                    $skuName = apiCall(ProductSkuApi::GET_SKU_NAME,[$id,'sku_desc']);
                    if($skuName['status']){
                        $result['info']['sku_name'] = json_decode($skuName['info']);
                    }else {
                        $this->apiReturnErr($skuName['info']);
                    }
                    //规格详细信息
                    $skulist = apiCall(ProductSkuApi::QUERY_NO_PAGING,[['product_id' => $id]]);
                    $min_ori_price = 1e18;
                    $min_price     = 1e18;
                    $max_price     = 0.0;
                    $max_ori_price = 0.0;
                    $min_member_price_1 = 1e18;
                    $min_member_price_2 = 1e18;
                    if($uid){
                        $min_member_price = 1e18;
                    }else{
                        $min_member_price = $this->getMinPrice($pid);
                    }
                    $max_member_price = 0.0;
                    // trace();
                    if($skulist['status']){
                        $result['info']['sku_list'] = $skulist['info'];
                        foreach($result['info']['sku_list'] as &$vo){
                            //查询会员价
$r = apiCall(ProductMemberPriceApi::QUERY_NO_PAGING,[['sku_id'=>$vo['id']],false,'member_group_id,price']);
if(!$r['status']) $this->apiReturnErr($r['info']);
if(empty($r['info'])){
    return $this->apiReturnErr('请先设置该商品会员价!');
}else{
    foreach ($r['info'] as $v) {
        $group = (int) $v['member_group_id'];
        $vo['member_price_'.$group] = $v['price'];
        if($group === 1){
            $min_member_price_1 = min($v['price'],$min_member_price_1);
        }elseif($group === 2){
            $min_member_price_2 = min($v['price'],$min_member_price_2);
        }
    }
}
                            $vo['member_price'] = '';
                            if($uid){
                                $r = apiCall(ProductMemberPriceApi::GET_INFO_BY_SKUID,[$uid,$vo['id'],'price']);
                                if(!$r['status']) $this->apiReturnErr($r['info']);
                                if(!empty($r['info'])){
                                    $vo['member_price'] = $r['info']['price'];
                                    $min_member_price   = min($min_member_price,floatval($r['info']['price']));
                                    $max_member_price   = max($max_member_price,floatval($r['info']['price']));
                                }
                            }
                            //查询税费
                            $r = apiCall(ProductSkuApi::GET_TAX,[$id,$vo['id']]);
                            if(!$r['status']) $this->apiReturnErr($r['info']);
                            $vo['tax_rate'] = ''.$r['info'];
                            $min_ori_price = min($vo['ori_price'],$min_ori_price);
                            $max_ori_price = max($vo['ori_price'],$max_ori_price);
                            $min_price = min($vo['price'],$min_price);
                            $max_price = max($vo['price'],$max_price);
                            $count += intval($vo['quantity']);
                        }
                        $result['info']['min_price']     = $min_price;
                        $result['info']['min_ori_price'] = $min_ori_price;
                        $result['info']['max_price']     = $max_price;
                        $result['info']['max_ori_price'] = $max_ori_price;
                        $result['info']['min_member_price']  = ''.$min_member_price;
                        $result['info']['max_member_price']  = ''.$max_member_price;
                        $result['info']['min_member_price_1']  = ''.$min_member_price_1;
                        $result['info']['min_member_price_2']  = ''.$min_member_price_2;
                    }else{
                        $this -> apiReturnErr($skulist['info']);
                    }

                }else{
                    //单规格查询
                    $skulist = apiCall(ProductSkuApi::GET_INFO,[['product_id' => $id]]);
                    if($skulist['status']){
                        //查询会员价
                        if(empty($skulist['info'])) $this->apiReturnErr('该商品还未添加规格');
$r = apiCall(ProductMemberPriceApi::QUERY_NO_PAGING,[['sku_id'=>$skulist['info']['id']],false,'member_group_id,price']);
if(!$r['status']) $this->apiReturnErr($r['info']);
if(empty($r['info'])){
    return $this->apiReturnErr('请先设置该商品会员价!');
}else{
    foreach ($r['info'] as $v) {
        $skulist['info']['member_price_'.$v['member_group_id']] = $v['price'];
    }
}
                        if($uid){
                            $r = apiCall(ProductMemberPriceApi::GET_INFO_BY_SKUID,[$uid,$skulist['info']['id'],'price']);
                            if(!$r['status']) $this->apiReturnErr($r['info']);
                            if(!empty($r['info'])) $skulist['info']['member_price'] = $r['info']['price'];
                            else $this->apiReturnErr('该商品会员价未设置!');
                        }else{
                            $skulist['info']['member_price'] = $this->getMinPrice($id);
                        }
                        //查询税费
                        $r = apiCall(ProductSkuApi::GET_TAX,[$id,$skulist['info']['id']]);
                        if(!$r['status']) $this->apiReturnErr($r['info']);
                        $skulist['info']['tax_rate'] = ''.$r['info'];
                        unset($skulist['info']['sku_id']);
                        unset($skulist['info']['sku_desc']);
                        unset($skulist['info']['createtime']);
                        unset($skulist['info']['icon_url']);
                        $result['info']['sku_info']      = $skulist['info'];
                        $result['info']['min_ori_price'] = $skulist['info']['ori_price'];
                        $result['info']['min_price']     = $skulist['info']['price'];
                        $result['info']['min_member_price'] = $skulist['info']['member_price'];
                        if(!isset($skulist['info']['member_price_1'])){
                            $this->apiReturnErr('未设置会员价1');
                        }
                        if(!isset($skulist['info']['member_price_2'])){
                            $this->apiReturnErr('未设置会员价2');
                        }
                        $result['info']['min_member_price_1'] = $skulist['info']['member_price_1'];
                        $result['info']['min_member_price_2'] = $skulist['info']['member_price_2'];
                        $count = $skulist['info']['quantity'];
                    }else{
                        $this -> apiReturnErr($skulist['info']);
                    }
                }

                $result['info']['remain'] = max(intval($count) - intval($result['info']['total_sales']),0);
                //查询商品分组信息
                // $group = apiCall(ProductGroupApi::GET_GROUP_BY_ID,[$id]);
                // if(!$group['status']){
                //     $this -> apiReturnErr($group['info']);
                // }
                // $result['info']['group'] = $group['info'];
                // if(is_null($result['info']['group'])) unset($result['info']['group']);
                //查询用户是否收藏该商品
                $result['info']['is_fav'] = 0;
                if($uid != 0){
                    $map = [
                        'favorite_id' => $id,
                        'uid'         => $uid,
                    ];
                    $favorite = apiCall(FavoritesApi::COUNT,[$map]);
                    if($favorite['status']){
                        if($favorite['info']>0){
                            $result['info']['is_fav'] = 1;
                        }
                    }else{
                        $this -> apiReturnErr($favorite['info']);
                    }
                }

                //查询商品套餐信息
                $r = apiCall(ProductPackageItemApi::QUERY_BY_PID,[$id]);

                if(!$r['status']) $this->apiReturnErr($r['info']);
                $p_infos = []; //缓存查过的
                foreach ($r['info'] as &$v) {
                    $p_id   = $v['p_id'];
                    $sku_id = intval($v['sku_id']);
                    if(!isset($p_infos[$p_id])) $p_infos[$p_id] = [];
                    if(!isset($p_infos[$p_id][$sku_id])) $p_infos[$p_id][$sku_id]=[];

                    $i_info = $p_infos[$p_id][$sku_id];
                    //查询 price,ori_price,sku_desc,icon_url
                    if(!isset($i_info['price'])){
                        $res = apiCall(ProductSkuApi::GET_INFO_BY_SKUID,[$p_id,$sku_id]);
                        if(!$res['status']) $this->apiReturnErr($res['info']);
                        $i_info = $res['info'];
                        $p_infos[$p_id][$sku_id] = $i_info;
                    }
                    if(!$i_info) $this->apiReturnErr('套餐中所含规格('.$sku_id.')已失效,请修改套餐');
                    $v['price']     = $i_info['price'];
                    $v['ori_price'] = $i_info['ori_price'];
                    $v['sku_desc']  = $sku_id ? $i_info['sku_desc'] : '';
                    if($sku_id) $v['img_id'] = $i_info['icon_url'];
                }
                $p_infos = [];
                foreach ($r['info'] as $v) {
                    $pack_id = $v['id'];
                    if(!isset($p_infos[$pack_id])){
                        $p_infos[$pack_id] = [
                            'id'            =>$v['id'],
                            'package_name'  =>$v['package_name'],
                            'create_time'   =>$v['create_time'],
                            'end_time'      =>$v['end_time'],
                            'package_price' =>$v['package_price'],
                        ];
                    }
                    $p_infos[$pack_id]['pro_list'][] = [
                      "p_id"      => $v['p_id'],
                      "sku_id"    => $v['sku_id'],
                      "is_main"   => $v['is_main'],
                      "name"      => $v['name'],
                      "price"     => $v['price'],
                      "ori_price" => $v['ori_price'],
                      "sku_desc"  => $v['sku_desc'],
                    ];
                }
                //按套餐排列
                // $this->apiReturnSuc($r['info']);
                $result['info']['package_list'] = array_values($p_infos);
                unset($p_infos);

                //查询商品收藏总数
                $map = ['favorite_id' => $id];
                $favorite = apiCall(FavoritesApi::COUNT,[$map]);
                if($favorite['status']){
                    $result['info']['favs'] = $favorite['info'];
                }else{
                    $this -> apiReturnErr($favorite['info']);
                }
                $this -> apiReturnSuc($result['info']);
            }
        }
    }

    /**
     * 私有方法
     * @param $id
     */
    private function getMinPrice($pid){
        $r = apiCall(ProductSkuApi::GET_MIN_PRICE,[$pid]);
        if(!$r['status']) $this->apiReturnErr($r['info']);
        return $r['info'];
    }

    private function getContent($id)
    {
        $result = apiCall(ProductApi::DETAIL, [$id]);

        if ($result['status']) {
            if ($result['info']['status'] == -1 || $result['info']['onshelf'] == 0) {
                $this->apiReturnErr('商品id参数错误');
            }
            //清除不用字段
            unset($result['info']['uid']);
            unset($result['info']['loc_country']);
            unset($result['info']['loc_province']);
            unset($result['info']['loc_city']);
            unset($result['info']['loc_address']);
            unset($result['info']['createtime']);
            unset($result['info']['updatetime']);
            unset($result['info']['onshelf']);
            unset($result['info']['status']);
            unset($result['info']['store_id']);
            unset($result['info']['weight']);
            unset($result['info']['synopsis']);
            unset($result['info']['dt_origin_country']);
            unset($result['info']['_price']);
            unset($result['info']['dt_goods_unit']);
            unset($result['info']['pid']);
            unset($result['info']['detail']);

            $has_sku = apiCall(ProductSkuApi::HAS_SKU, array($id));

            if ($has_sku['status']) {
                //0为统一规格,1为多规格
                $has_sku = $has_sku['info'];
            } else {
                $this->apiReturnErr($has_sku['info']);
            }
            $result['info']['has_sku'] = $has_sku;

            if($has_sku == 1){
                //多规格传值
                //规格id
                $skuId = apiCall(ProductSkuApi::GET_SKU_ID,array( $id , 'sku_id' ));
                if($skuId['status']){
                    $result['info']['sku_id'] = json_decode($skuId['info']);
                }else{
                    $this -> apiReturnErr($skuId['info']);
                }
                //规格名称
                $skuName = apiCall(ProductSkuApi::GET_SKU_NAME,array( $id , 'sku_desc' ));
                if($skuName['status']){
                    $result['info']['sku_name'] = json_decode($skuName['info']);
                }else {
                    $this->apiReturnErr($skuName['info']);
                }
                //规格详细信息
                $skulist = apiCall(ProductSkuApi::QUERY_NO_PAGING,array(array('product_id' => $id)));
                $min_ori_price = 1e18;
                $min_price = 1e18;
                $max_price = 0.0;
                $max_ori_price = 0.0;
                if($skulist['status']){
                    $result['info']['sku_list'] = $skulist['info'];
                    foreach($skulist['info'] as $vo){
                        if($vo['ori_price'] < $min_ori_price){
                            $min_ori_price = $vo['ori_price'];
                        }
                        if($vo['ori_price'] > $max_ori_price){
                            $max_ori_price = $vo['ori_price'];
                        }
                        if($vo['price'] < $min_price){
                            $min_price = $vo['price'];
                        }
                        if($vo['price'] > $max_price){
                            $max_price = $vo['price'];
                        }
                    }
                    $result['info']['min_price'] = $min_price;
                    $result['info']['min_ori_price'] = $min_ori_price;
                    $result['info']['max_price'] = $max_price;
                    $result['info']['max_ori_price'] = $max_ori_price;
                }else{
                    $this -> apiReturnErr($skulist['info']);
                }

            }else{
                $skulist = apiCall(ProductSkuApi::GET_INFO,array(array('product_id' => $id)));
                if($skulist['status']){
                    unset($skulist['info']['sku_id']);
                    unset($skulist['info']['sku_desc']);
                    unset($skulist['info']['createtime']);
                    unset($skulist['info']['icon_url']);
                    $result['info']['sku_list'] = $skulist['info'];
                    $result['info']['min_ori_price'] = $skulist['info']['ori_price'];
                    $result['info']['min_price'] = $skulist['info']['price'];
                }else{
                    $this -> apiReturnErr($skulist['info']);
                }
            }

            $group = apiCall(ProductGroupApi::GET_GROUP_BY_ID,array($id));
            if(!$group['status']){
                $this -> apiReturnErr($group['info']);
            }
            $result['info']['group'] = $group['info'];

            $result['info']['favorite'] = 0;

            return $result['info'];

        } else {
            $this->apiReturnErr($result['info']);
        }

    }

    /**
     * 循环版本
     * @param $id
     * @return array
     */
    /*private function queryChild($id){
        $que = [];
        array_push($que,$id);
        while(intval($id)){
            // $tmp = array_shift($que);
            $map = ['parent' => $id];
            $result = apiCall(CategoryApi::QUERY_NO_PAGING,[[$map]]);

            if($result['status']){
                if(($result['info'])){
                    foreach($result['info'] as $val){
                       array_push($que,$val['id']);
                    }
                }else{
                    array_push($this -> cate , $tmp );
                }
            }else{
                return ['status' => false,'info' => $result['info']];
            }
        }
        return ['status' => true,'info' => $this -> cate];
    }*/

    /**
     * 递归版本
     */
    private function queryChild($id){

        $map = [
           'parent' => $id
        ];
        $result = apiCall(CategoryApi::QUERY_NO_PAGING,[$map,false,'id']);
        if($result['status']){

            if(!empty($result['info'])){
                foreach($result['info'] as $val){
                    $this->queryChild($val['id']);
                }
            }else{
                array_push($this->cate,$id);
            }
        }
    }
    /**
     * 获取评论附件
     * @param  int   $cid  评论ID
     * @return [type]      [description]
     */
    private function getAttachs($cid,$img=3,&$arr){
        $map = ['comment_id'=>$cid];
        $result = apiCall(OrderCommentAttachsApi::QUERY_NO_PAGING,[$map,false,['pic_id']]);
        if($result['status']){
            $arr['img'] = [];
            foreach($result['info'] as $key=>$v){
                if($key >$img) break;
                array_push($arr['img'],$v['pic_id']);
            }

        }
    }
    /**
     * 查询用户信息或检查用户是否存在
     */
    private function getUserInfo($uid){
        $r = apiCall(MemberApi::GET_INFO,[['uid'=>$uid]]);
        if($r['status'] && $r['info']){
            return $r['info'];
        }else{
            return [];//$this->apiReturnErr('查询用户信息失败');
        }
    }
}